﻿using Caliburn.Micro;
using BoriPeter.FitnessJournal8.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BoriPeter.FitnessJournal8.ViewModels
{
    public class AddNewWeightViewModel : ViewModelBase
    {
        #region Constructor
        public AddNewWeightViewModel(INavigationService navigationService)
            : base(navigationService)
        {

        }
        #endregion

        #region OnGoBack()
        protected async override void OnGoBack()
        {
            base.OnGoBack();

            await NutritionSource.SaveAsync(Entry);
        }
        #endregion

        #region OnInitialize
        protected override async void OnInitialize()
        {
            Entry = new DailyEntry();
            var entries = await NutritionSource.GetDailyEntryListAsync(DateTime.Now.AddMonths(-1), DAL.EntryService.Weight);

            DateList = new List<DateNamed>(DateNamed.DefaultList);
            var lastEntry = entries.Where(e => e.WeightKg != 0).OrderByDescending(e => e.Date).First();
            var nextDate = lastEntry.Date.AddDays(1);
            DateNamed newNextDate = new DateNamed(nextDate.ToString("yyyy-MM-dd"), nextDate);
            DateList.Add(newNextDate);
            //SelectedDate = newNextDate;

            Entry.WeightKg = lastEntry.WeightKg;
        }
        #endregion
            
        #region Date
        private List<DateNamed> dateList;
        public List<DateNamed> DateList
        {
            get
            {
                return this.dateList;
            }

            set
            {
                this.dateList = value;
                NotifyOfPropertyChange("DateList");
            }
        }

        private DateNamed selectedDate;
        public DateNamed SelectedDate
        {
            get
            {
                return this.selectedDate;
            }

            set
            {
                this.selectedDate = value;
                NotifyOfPropertyChange("SelectedDate");
            }
        }
        #endregion

        #region Entry
        private DailyEntry entry;
        public DailyEntry Entry
        {
            get
            {
                return this.entry;
            }

            set
            {
                this.entry = value;
                NotifyOfPropertyChange("Entry");
            }
        }
        #endregion
    }
}
